package com.example.jpush;

import android.content.Context;
import android.os.Handler;
import android.util.SparseArray;

import java.util.Set;

import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;

/**
 * 极光推送管理工具类
 *
 * @author Zml
 * @date 2018/3/14.
 */
public class LKJPushManager {
    private static final String TAG = "JPUSH-LKJPushManager";
    private static LKJPushManager mInstance;
    /**
     * 增加
     */
    public static final int ACTION_ADD = 1;
    /**
     * 覆盖
     */
    public static final int ACTION_SET = 2;
    /**
     * 删除部分
     */
    public static final int ACTION_DELETE = 3;
    /**
     * 删除所有
     */
    public static final int ACTION_CLEAN = 4;
    /**
     * 查询
     */
    public static final int ACTION_GET = 5;

    public static final int ACTION_CHECK = 6;

    private TagAliasBean mTagAlias = new TagAliasBean();

    public static int sequence = 1;
    private SparseArray<TagAliasBean> mTagAliasCache = new SparseArray<>();

    /**
     * 获取 LKJPushManager 实例
     *
     * @return mInstance
     */
    public static LKJPushManager getInstance() {
        if (mInstance == null) {
            synchronized (LKJPushManager.class) {
                if (mInstance == null) {
                    mInstance = new LKJPushManager();
                }
            }
        }
        return mInstance;
    }

    /**
     * 初始化极光，一般可以放到程序的启动Activity或是Application的onCreate方法中调用
     *
     * @param debugMode 设置是否开启日志,发布时请关闭日志
     */
    public void initJPush(Context context, boolean debugMode) {
        // 设置开启日志,发布时请关闭日志
        JPushInterface.setDebugMode(debugMode);
        // 初始化 JPush
        JPushInterface.init(context);
    }

    /**
     * 停止推送，一般是程序退出登录时候
     */
    public void stopJPuth(Context context) {
        JPushInterface.stopPush(context);
    }

    /**
     * 恢复推送服务
     */
    public void resumePush(Context context) {
        JPushInterface.resumePush(context);
    }

    /**
     * 检查 Push Service 是否已经停止
     *
     * @param context
     * @return
     */
    public boolean isPushStopped(Context context) {
        return JPushInterface.isPushStopped(context);
    }

    /**
     * 设置极光推送app别名
     * 用于给某特定用户推送消息。别名，可以近似地被认为，是用户帐号里的昵称 使用标签
     *
     * @param alias
     */
    public void setAlias(Context context, String alias, Handler handler) {
        setTagAliasBean(ACTION_SET, alias, null, true);
//        setAliasAndTags(context, mTagAlias);
        JPushInterface.setAlias(context, alias, new tagCallBack(handler, alias));
    }

    private class tagCallBack implements TagAliasCallback {
        private Handler handler;
        private String alias;

        public tagCallBack(Handler handler, String alias) {
            this.handler = handler;
            this.alias = alias;
        }

        @Override
        public void gotResult(int code, String s, Set<String> set) {
            switch (code) {
                case 0:
                    // 建议这里往 SharePreference 里写一个成功设置的状态。成功设置一次后，以后不必再次设置了。
                    break;
                case 6002:
                    // 延迟 60 秒来调用 Handler 设置别名
                    if (null != handler) {
                        handler.sendMessageDelayed(handler.obtainMessage(6002, alias), 1000 * 60);
                    }
                    break;
                default:
                    break;
            }
        }
    }


    /**
     * 删除极光推送app别名
     */
    public void deleteAlias(Context conn) {
        setTagAliasBean(ACTION_DELETE, null, null, true);
        setAliasAndTags(conn, mTagAlias);
    }

    /**
     * 获取极光推送app别名
     */
    public void getAlias(Context conn) {
        setTagAliasBean(ACTION_GET, null, null, true);
        setAliasAndTags(conn, mTagAlias);
    }

    /**
     * 设置标签
     * 用于给某一群人推送消息。标签类似于博客里为文章打上 tag ，即为某资源分类。
     */
    public void setTags(Context conn, Set<String> Tags) {
        setTagAliasBean(ACTION_SET, null, Tags, false);
        setAliasAndTags(conn, mTagAlias);
    }

    /**
     * 添加标签
     */
    public void addTags(Context conn, Set<String> Tags) {
        setTagAliasBean(ACTION_ADD, null, Tags, false);
        setAliasAndTags(conn, mTagAlias);
    }


    /**
     * 删除标签
     */
    public void deleteTags(Context conn, Set<String> Tags) {
        setTagAliasBean(ACTION_DELETE, null, Tags, false);
        setAliasAndTags(conn, mTagAlias);
    }

    /**
     * 删除所有标签
     */
    public void cleanTags(Context conn) {
        setTagAliasBean(ACTION_CLEAN, null, null, false);
        setAliasAndTags(conn, mTagAlias);
    }

    /**
     * 获取所有标签
     */
    public void getAllTags(Context conn) {
        setTagAliasBean(ACTION_GET, null, null, false);
        setAliasAndTags(conn, mTagAlias);
    }


    /**
     * 设置 TagAliasBean
     *
     * @param action        目标操作
     * @param alias
     * @param tags
     * @param isAliasAction true 设置alias ；false设置tags
     */
    private void setTagAliasBean(int action, String alias, Set<String> tags, boolean isAliasAction) {

        mTagAlias.action = action;
        mTagAlias.alias = alias;
        mTagAlias.tags = tags;
        mTagAlias.isAliasAction = isAliasAction;

    }

    /**
     * 清除所有通知
     */
    public void clearAllNotification(Context context) {
        JPushInterface.clearAllNotifications(context);
    }

    public void put(int sequence, TagAliasBean tagAliasBean) {
        mTagAliasCache.put(sequence, tagAliasBean);
    }

    public void setAliasAndTags(Context context, TagAliasBean tagAliasBean) {
        if (tagAliasBean == null) {
            return;
        }
        sequence++;
        put(sequence, tagAliasBean);
        if (tagAliasBean.isAliasAction) {
            switch (tagAliasBean.action) {
                case ACTION_GET:
                    JPushInterface.getAlias(context, sequence);
                    break;
                case ACTION_DELETE:
                    JPushInterface.deleteAlias(context, sequence);
                    break;
                case ACTION_SET:
                    JPushInterface.setAlias(context, sequence, tagAliasBean.alias);
                    break;
                default:
            }
        } else {
            switch (tagAliasBean.action) {
                case ACTION_ADD:
                    JPushInterface.addTags(context, sequence, tagAliasBean.tags);
                    break;
                case ACTION_SET:
                    JPushInterface.setTags(context, sequence, tagAliasBean.tags);
                    break;
                case ACTION_DELETE:
                    JPushInterface.deleteTags(context, sequence, tagAliasBean.tags);
                    break;
                case ACTION_CHECK:
                    String tag = (String) tagAliasBean.tags.toArray()[0];
                    JPushInterface.checkTagBindState(context, sequence, tag);
                    break;
                case ACTION_GET:
                    JPushInterface.getAllTags(context, sequence);
                    break;
                case ACTION_CLEAN:
                    JPushInterface.cleanTags(context, sequence);
                    break;
                default:
            }
        }
    }


    public static class TagAliasBean {
        int action;
        Set<String> tags;
        String alias;
        boolean isAliasAction;

        @Override
        public String toString() {
            return "TagAliasBean{" +
                    "action=" + action +
                    ", tags=" + tags +
                    ", alias='" + alias + '\'' +
                    ", isAliasAction=" + isAliasAction +
                    '}';
        }
    }

}
